<?php
class JoblistForm extends BaseForm {
	public $jobs;
	public $selected; 
	
	public function JoblistForm() {
		parent::BaseForm();
		
		$this->clear();
	}
	
	public function clear(){
		$this->jobs = array();
		$this->selected = array();
	}
	
	public function getJobs(){
		return $this->jobs;
	}
	
	public function getSelected(){
		return $this->selected;
	}
	
	public function setSelected($val){
		$this->selected = $val;
	}
	
	public function setJobs($val){
		$this->jobs = $val;
	}
	
	public function deleteJobs($ids){
		$arr = array();
		$keys = array_flip($ids);
		
		foreach($this->jobs as $current){
			$id = $current->getId();
			
			if(! array_key_exists($id, $keys)){
				$arr[] = $current; 
			}
		}
		
		$this->setJobs($arr);
		$this->setSelected(array());
	}
	
	public function addJob($job){
		if($job != null){
			$this->jobs[] = $job;
		}
		else {
			throw new Exception('You are trying to add a null job');
		} 
	}
	
	public function updateJob($job){
		$id = $job->getId();
		$found = $this->getJob($id);
		
		if($found != null){
			$found->copy($job);
		}
		else {
			throw new Exception('Cannot find job with the id: ' . $id);
		}
	}
	
	public function getJob($id){
		$res = null;
		
		foreach($this->jobs as $current){
			$crtId = $current->getId();
			
			if($id == $crtId){
				$res = $current; 
				
				break;
			}
		}
		
		return $res;
	}
	
} 
?>